/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.fa.supporters;

import javax.swing.table.AbstractTableModel;

/**
 *
 * @author tatung
 */
public class ComboBoxTableModel extends AbstractTableModel {
  // Implementation of TableModel interface
    public int getRowCount() {
        return data.length;
    }

    public int getColumnCount() {
        return COLUMN_COUNT;
    }

    public Object getValueAt(int row, int column) {
        return data[row][column];
    }

    public Class getColumnClass(int column) {
        return (data[0][column]).getClass();
    }

    public String getColumnName(int column) {
        return columnNames[column];
    }

    public boolean isCellEditable(int row, int column) {
        return column == 1;
    }

    public void setValueAt(Object value, int row, int column) {
        if (isValidValue(value)) {
          data[row][column] = value;
          fireTableRowsUpdated(row, row);
        }
    }

    // Extra public methods
    public static String[] getValidStates() {
        return validStates;
    }

    // Protected methods
    protected boolean isValidValue(Object value) {
        if (value instanceof String) {
            String sValue = (String)value;

            for (int i = 0; i < validStates.length; i++) {
                if (sValue.equals(validStates[i])) {
                    return true;
                }
            }
        }

        return false;
    }

    protected static final int COLUMN_COUNT = 2;

    protected static final String[] validStates = {
    "On order", "In stock", "Out of print"
    };

    protected Object[][] data = new Object[][] {
    { "Core Java Volume 1", validStates[0] },
    { "Core Java Volume 2", validStates[0] },
    { "Core Web Programming", validStates[0] },
    { "Core Visual Basic 5", validStates[0] },
    { "Core Java Foundation Classes", validStates[0] }
    };

    protected static final String[] columnNames = {
    "Book Name", "Status"
    };
}
